{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import matplotlib\n",
    "import numpy as np\n",
    "np.set_printoptions(threshold=np.nan)\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm import tqdm_notebook as tqdm\n",
    "import networkx as nx\n",
    "import scipy.sparse as sparse\n",
    "import asizeof\n",
    "n_jobs=22"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def simulation(n,m,p,init_Theta,N,r,prob):\n",
    "    G=nx.barabasi_albert_graph(n, p)\n",
    "    G=G.to_directed()\n",
    "    for u,v in G.edges():\n",
    "        G[u][v]['weight']=np.random.uniform(0,1)\n",
    "    for i in range(n):\n",
    "        in_degree=G.in_degree(i,weight='weight')\n",
    "        for u,v in G.in_edges(i):\n",
    "            G[u][v]['weight']/=in_degree\n",
    "    W=nx.adjacency_matrix(G,weight='weight')\n",
    "    C = np.random.random((m, m))\n",
    "    C=C*2-1\n",
    "    C *= np.tri(*C.shape,k=-1)\n",
    "    C=C+np.transpose(C)+np.eye(m)\n",
    "    indykatory=[]\n",
    "    frakcje=[]\n",
    "    I=np.random.choice(a=[0,1], size=(n,m), replace=True, p=[1-prob, prob])\n",
    "    indykatory.append(I)\n",
    "    frakcje.append(np.vstack((range(m),np.sum(I,axis=0)/n)))\n",
    "    I=I.copy()\n",
    "    Theta=np.full((n,m), init_Theta)\n",
    "    Y=np.full((n,1),0)\n",
    "    for l in range(N):    \n",
    "        U=W.transpose().dot(I)\n",
    "        F=U.dot(C)/m\n",
    "        temp=np.greater_equal(F, Theta)\n",
    "        for i in np.unique(np.where(temp[:,:]==True)[0]):\n",
    "            temp1=np.where(temp[i,:]==True)[0]\n",
    "            #print(i,temp1.shape,l)\n",
    "            if not np.any(C[temp1[:, None],temp1]<0):\n",
    "                I[i][temp1]=1\n",
    "                Y[i]+=1\n",
    "                Theta[i]=1-(1-init_Theta)**Y[i]\n",
    "            else:\n",
    "                if np.random.rand()<r:\n",
    "                    I[i][temp1]=1\n",
    "                    Y[i]+=1\n",
    "                    Theta[i]=1-(1-init_Theta)**Y[i]\n",
    "        indykatory.append(I)\n",
    "        frakcje.append(np.vstack((range(m),np.sum(I,axis=0)/n)))\n",
    "        I=I.copy()\n",
    "    del temp\n",
    "    del temp1\n",
    "    return [frakcje,indykatory]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from joblib import Parallel, delayed\n",
    "import time\n",
    "def text_progessbar(seq, total=None):\n",
    "    step = 1\n",
    "    tick = time.time()\n",
    "    while True:\n",
    "        time_diff = time.time()-tick\n",
    "        avg_speed = time_diff/step\n",
    "        total_str = 'of %n' % total if total else ''\n",
    "        print('step', step, '%.2f' % time_diff, 'avg: %.2f iter/sec' % avg_speed, total_str)\n",
    "        step += 1\n",
    "        yield next(seq)\n",
    "all_bar_funcs = {\n",
    "    'txt': lambda args: lambda x: text_progessbar(x, **args),\n",
    "    'None': lambda args: iter,\n",
    "}\n",
    "def ParallelExecutor(use_bar='tqdm', **joblib_args):\n",
    "    def aprun(bar=use_bar, **tq_args):\n",
    "        def tmp(op_iter):\n",
    "            if str(bar) in all_bar_funcs.keys():\n",
    "                bar_func = all_bar_funcs[str(bar)](tq_args)\n",
    "            else:\n",
    "                raise ValueError(\"Value %s not supported as bar type\"%bar)\n",
    "            return Parallel(**joblib_args)(bar_func(op_iter))\n",
    "        return tmp\n",
    "    return aprun\n",
    "\n",
    "aprun = ParallelExecutor(n_jobs=n_jobs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "step 1 0.00 avg: 0.00 iter/sec \n",
      "step 2 0.00 avg: 0.00 iter/sec \n",
      "step 3 0.01 avg: 0.00 iter/sec \n",
      "step 4 0.01 avg: 0.00 iter/sec \n",
      "step 5 0.01 avg: 0.00 iter/sec \n",
      "step 6 0.01 avg: 0.00 iter/sec \n",
      "step 7 0.02 avg: 0.00 iter/sec \n",
      "step 8 0.02 avg: 0.00 iter/sec \n",
      "step 9 0.02 avg: 0.00 iter/sec \n",
      "step 10 0.02 avg: 0.00 iter/sec \n",
      "step 11 0.02 avg: 0.00 iter/sec \n",
      "step 12 0.03 avg: 0.00 iter/sec \n",
      "step 13 0.03 avg: 0.00 iter/sec \n",
      "step 14 0.03 avg: 0.00 iter/sec \n",
      "step 15 0.03 avg: 0.00 iter/sec \n",
      "step 16 0.03 avg: 0.00 iter/sec \n",
      "step 17 0.03 avg: 0.00 iter/sec \n",
      "step 18 0.03 avg: 0.00 iter/sec \n",
      "step 19 0.03 avg: 0.00 iter/sec \n",
      "step 20 0.04 avg: 0.00 iter/sec \n",
      "step 21 0.04 avg: 0.00 iter/sec \n",
      "step 22 0.04 avg: 0.00 iter/sec \n",
      "step 23 0.04 avg: 0.00 iter/sec \n",
      "step 24 0.04 avg: 0.00 iter/sec \n",
      "step 25 0.04 avg: 0.00 iter/sec \n",
      "step 26 0.04 avg: 0.00 iter/sec \n",
      "step 27 0.04 avg: 0.00 iter/sec \n",
      "step 28 0.05 avg: 0.00 iter/sec \n",
      "step 29 0.05 avg: 0.00 iter/sec \n",
      "step 30 0.05 avg: 0.00 iter/sec \n",
      "step 31 0.05 avg: 0.00 iter/sec \n",
      "step 32 0.05 avg: 0.00 iter/sec \n",
      "step 33 0.05 avg: 0.00 iter/sec \n",
      "step 34 0.05 avg: 0.00 iter/sec \n",
      "step 35 0.05 avg: 0.00 iter/sec \n",
      "step 36 0.06 avg: 0.00 iter/sec \n",
      "step 37 0.06 avg: 0.00 iter/sec \n",
      "step 38 0.06 avg: 0.00 iter/sec \n",
      "step 39 0.06 avg: 0.00 iter/sec \n",
      "step 40 0.06 avg: 0.00 iter/sec \n",
      "step 41 0.06 avg: 0.00 iter/sec \n",
      "step 42 0.06 avg: 0.00 iter/sec \n",
      "step 43 0.06 avg: 0.00 iter/sec \n",
      "step 44 0.06 avg: 0.00 iter/sec \n",
      "step 45 13.75 avg: 0.31 iter/sec \n",
      "step 46 14.09 avg: 0.31 iter/sec \n",
      "step 47 14.46 avg: 0.31 iter/sec \n",
      "step 48 15.25 avg: 0.32 iter/sec \n",
      "step 49 15.56 avg: 0.32 iter/sec \n",
      "step 50 15.78 avg: 0.32 iter/sec \n",
      "step 51 16.01 avg: 0.31 iter/sec \n",
      "step 52 16.26 avg: 0.31 iter/sec \n",
      "step 53 16.71 avg: 0.32 iter/sec \n",
      "step 54 17.09 avg: 0.32 iter/sec \n",
      "step 55 17.59 avg: 0.32 iter/sec \n",
      "step 56 18.04 avg: 0.32 iter/sec \n",
      "step 57 18.57 avg: 0.33 iter/sec \n",
      "step 58 19.02 avg: 0.33 iter/sec \n",
      "step 59 19.67 avg: 0.33 iter/sec \n",
      "step 60 20.14 avg: 0.34 iter/sec \n",
      "step 61 20.48 avg: 0.34 iter/sec \n",
      "step 62 21.32 avg: 0.34 iter/sec \n",
      "step 63 21.80 avg: 0.35 iter/sec \n",
      "step 64 22.66 avg: 0.35 iter/sec \n",
      "step 65 23.66 avg: 0.36 iter/sec \n",
      "step 66 24.36 avg: 0.37 iter/sec \n",
      "step 67 24.79 avg: 0.37 iter/sec \n",
      "step 68 25.50 avg: 0.38 iter/sec \n",
      "step 69 26.07 avg: 0.38 iter/sec \n",
      "step 70 26.67 avg: 0.38 iter/sec \n",
      "step 71 27.17 avg: 0.38 iter/sec \n",
      "step 72 27.66 avg: 0.38 iter/sec \n",
      "step 73 28.61 avg: 0.39 iter/sec \n",
      "step 74 29.22 avg: 0.39 iter/sec \n",
      "step 75 29.77 avg: 0.40 iter/sec \n",
      "step 76 30.42 avg: 0.40 iter/sec \n",
      "step 77 31.08 avg: 0.40 iter/sec \n",
      "step 78 31.55 avg: 0.40 iter/sec \n",
      "step 79 32.42 avg: 0.41 iter/sec \n",
      "step 80 33.02 avg: 0.41 iter/sec \n",
      "step 81 33.82 avg: 0.42 iter/sec \n",
      "step 82 34.66 avg: 0.42 iter/sec \n",
      "step 83 35.47 avg: 0.43 iter/sec \n",
      "step 84 36.34 avg: 0.43 iter/sec \n",
      "step 85 37.35 avg: 0.44 iter/sec \n",
      "step 86 37.78 avg: 0.44 iter/sec \n",
      "step 87 38.30 avg: 0.44 iter/sec \n",
      "step 88 38.63 avg: 0.44 iter/sec \n",
      "step 89 39.00 avg: 0.44 iter/sec \n",
      "step 90 39.91 avg: 0.44 iter/sec \n",
      "step 91 40.47 avg: 0.44 iter/sec \n"
     ]
    }
   ],
   "source": [
    "results=aprun(bar='txt')(delayed(simulation)(10000,100,3,i,10,0.1,0.2) for i in np.arange(0.01,0.055,0.0005))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0002509653568267822"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asizeof.asizeof(results)/1024/1024/1024"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
